package com.epoint.mobileoa.ui.wsgw;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.DialogInterface.OnMultiChoiceClickListener;
import android.content.Intent;
import android.text.Html;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.epoint.mobileoa.R;
import com.epoint.mobileoa.bizlogic.SuperView;
import com.epoint.mobileoa.tools.AlertHelp;
import com.epoint.mobileoa.tools.ControlsHelp;
import com.epoint.mobileoa.tools.StringHelp;
import com.epoint.mobileoa.tools.ToastHelp;
import com.epoint.mobileoa.tools.XMLUtil;
import com.epoint.mobileoa.ui.personchoose.NewPersonChooseView;
import com.epoint.mobileoa.ui.wsgw.model.Handle_GetUnHandleList_Model;
import com.epoint.mobileoa.ui.wsgw.model.NextStepInfoModel;
import com.epoint.mobileoa.ui.wsgw.model.UserInfoModel;
import com.epoint.mobileoa.ui.wsgw.model.WsOpinionModel;
import com.epoint.mobileoa.ui.wsgw.task.GetOpinionMubanTask;
import com.epoint.mobileoa.ui.wsgw.task.PostOpinionTask;

/**
 * @author liyc
 * @time 2012-12-19 上午9:09:12
 * @annotation 
 */
public class WsNextStepSignView extends SuperView {

	private TextView task_progress,title1;
	private NextStepInfoModel nm;
	private Button btPersonChoose,opitionButton,cleanButton,deal_btn;
	private EditText etPersonChoose,task_opinion;
	private ImageView ivDeletePersonChoose;
	private List<WsOpinionModel> comList = new ArrayList<WsOpinionModel>();
	private List<WsOpinionModel> priList = new ArrayList<WsOpinionModel>();
	private int mubanType;
	String opinion = "";
	String[] mbs;
	Handle_GetUnHandleList_Model h;
	@Override
	public void initControls() {
		cleanButton = (Button) findViewById(R.id.cleanButton);
		cleanButton.setOnClickListener(this);
		deal_btn = (Button) findViewById(R.id.deal_btn);
		deal_btn.setOnClickListener(this);
		task_opinion = (EditText) findViewById(R.id.task_opinion);
		opitionButton = (Button) findViewById(R.id.opitionButton);
		opitionButton.setOnClickListener(this);
		ivDeletePersonChoose = (ImageView) findViewById(R.id.ivDeletePersonChoose);
		ivDeletePersonChoose.setOnClickListener(this);
		etPersonChoose = (EditText) findViewById(R.id.etPersonChoose);
		nm = (NextStepInfoModel) getIntent().getSerializableExtra("nextstepmodel");
		h = (Handle_GetUnHandleList_Model) getIntent().getSerializableExtra("Handle_GetUnHandleList_Model");
		System.out.println("过滤人数："+nm.filterHandleUserList.size());
		System.out.println("默认人数："+nm.defaultHandleUserList.size());

		task_progress = (TextView) findViewById(R.id.task_progress);
		title1 = (TextView) findViewById(R.id.title1);
		task_progress.setText(getIntent().getStringExtra("currentStep"));
		title1.setText(Html.fromHtml("下一步<font color=red>【"+nm.StepName+"】</font>处理人"));
		btPersonChoose = (Button) findViewById(R.id.btPersonChoose);
		btPersonChoose.setOnClickListener(this);
		if(nm.CanEditUser.equals("0"))
		{
			ivDeletePersonChoose.setVisibility(View.GONE);
			btPersonChoose.setVisibility(View.GONE);
			if(nm.CanEditUser.equals("0")&&nm.defaultHandleUserList.size()==0)
			{
				etPersonChoose.setVisibility(View.GONE);
			}
		}

		if(nm.defaultHandleUserList.size()>0)
		{
			for(UserInfoModel u:nm.defaultHandleUserList)
			{
				etPersonChoose.setText(u.UserName+";");
				etPersonChoose.setTag(u.UserGuid+";");
			}
		}

	}


	@Override
	public void onClick(View v) {
		if(btPersonChoose==v)
		{
			if(nm.filterHandleUserList.size()>0)
			{
				String guids = "";
				for(UserInfoModel u:nm.filterHandleUserList)
				{
					guids += u.UserGuid+";";
				}
				NewPersonChooseView.choosePersonFilter(this, etPersonChoose, 1001, guids);
			}
			else{
				NewPersonChooseView.choosePerson(this, etPersonChoose,1001);
			}
		}
		else if(ivDeletePersonChoose==v)
		{
			etPersonChoose.setText("");
			etPersonChoose.setTag("");
		}
		else if(opitionButton==v)
		{
			AlertHelp.showAlertMenu(this, "选择意见模板", new String[]{"公共意见模板","个人意见模板"}, new OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					mubanType = which;
					if(comList.size()==0&&comList.size()==0)
					{
						HashMap<String, Object> param = new HashMap<String, Object>();
						new GetOpinionMubanTask(WsNextStepSignView.this,1, param, null, 2);
					}
					else{
						invokeMubanDialog();
					}
				}
			});
			/*
			new QuickActionDialog(
					this, 
					v, 
					null,
					new String[]{"公共意见模板","个人意见模板"}, 
					new QuickActionDialog.OnItemClickListener()
					{
						@Override
						public void onClick(int position) {
							mubanType = position;
							if(comList.size()==0&&comList.size()==0)
							{
								HashMap<String, Object> param = new HashMap<String, Object>();
								new GetOpinionMubanTask(WsNextStepSignView.this,1, param, null, 2);
							}
							else{
								invokeMubanDialog();
							}
						}
					}
					);
		*/}
		else if(v==cleanButton)
		{
			task_opinion.setText("");
		}
		else if(deal_btn==v)
		{
			if(etPersonChoose.getVisibility()==View.VISIBLE){
				if(ControlsHelp.Check_EditText_Blank_Warning(etPersonChoose, "请选择下一步处理人"))
				{
					return;
				}
			}
			HashMap<String, Object> param = new HashMap<String, Object>();
			param.put("MessageItemGuid",h.MessageItemGuid);
			param.put("NextStepGuid", nm.StepGuid);
			param.put("NextPersonGuid", etPersonChoose.getTag()==null?"":etPersonChoose.getTag());
			param.put("Opinion", task_opinion.getText().toString());
			new PostOpinionTask(this, 2, param, "正在提交下一步", 3);
		}
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode,
			Intent data) {
		System.out.println(requestCode);
		if (requestCode == 1001) {
			NewPersonChooseView.getData(data, etPersonChoose);
		}
	}

	@Override
	public void refresh(Object... param) throws Exception {
	}

	@Override
	public void refreshForNewTask(int TaskId,Object... param) {
		switch (TaskId) {
		case 1:
			String bs = param[0].toString();
			String comxml = StringHelp.getAttOut(bs, "commons");
			comList = XMLUtil.DomAnalysisCommon(comxml, WsOpinionModel.class);
			String prixml = StringHelp.getAttOut(bs, "persons");
			priList =  XMLUtil.DomAnalysisCommon(prixml, WsOpinionModel.class);
			invokeMubanDialog();
			break;
		case 2:
			String ReturnInfo = StringHelp.getXMLAtt(param[0].toString(), "ReturnInfo");
			String Status = StringHelp.getXMLAtt(ReturnInfo, "Status");
			if(Status.equals("True"))
			{
				ToastHelp.showToastCommon(this, "提交成功");
				finish();
				for(Activity a:fp.getAlist())
				{
					if(a.getClass().getName().contains("WsDbDetailView")||a.getClass().getName().contains("WsDbListView"))
					{
						a.finish();
					}
				}
			}
			else{
				ToastHelp.showAndroidToastWorning(this, StringHelp.getXMLAtt(ReturnInfo, "Description"));
			}
		default:
			break;
		}
	}

	private void invokeMubanDialog()
	{
		if(mubanType==0)
		{
			mbs = new String[comList.size()];
			for(int i=0;i<comList.size();i++)
			{
				mbs[i] = comList.get(i).text;
			}
		}
		else if(mubanType==1){
			mbs = new String[priList.size()];
			for(int i=0;i<priList.size();i++)
			{
				mbs[i] = priList.get(i).text;
			}
		}
		if(mbs.length>0){
			Builder builder=new AlertDialog.Builder(this);
			builder.setTitle("意见模板");
			builder.setMultiChoiceItems(mbs, null, new OnMultiChoiceClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which, boolean isChecked) {
					if(isChecked){
						if(!opinion.contains(mbs[which])){
							opinion += mbs[which]+"、";
						}
					}
				}
			});
			builder.setPositiveButton("确定", new OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					String pre = task_opinion.getText().toString();
					if(task_opinion.getText().toString().length()>0)
					{
						pre += "、";
					}
					task_opinion.setText(pre+opinion.substring(0, opinion.length()-1));
					opinion = "";
				}
			});
			builder.setNegativeButton("取消", null);
			AlertDialog dialog=builder.create();
			dialog.show();
		}
		else{
			ToastHelp.showAndroidToastWorning(this, "意见模板为空");
		}
	}

	@Override
	public int getLayoutInt() {
		return R.layout.wsnextstepsignview;
	}

}
